home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / borland / prbgi097.zip / C.ZIP / BGIDEMO.C < prev    next >
C/C++ Source or Header  |  1992-12-15  |  12KB  |  408 lines

  1. /* This program is not mine.
  2.    It was included with Turbo/Borland C(++)
  3.    and I only modified it a little  to demonstrate
  4.    features of my PRINTBGI library.
  5.    It is included here for demonstration only
  6.    and cannot be used for any other purposes. ( Could it? ).
  7.  
  8.    Original Copyright notice follows.
  9.  */
  10.  
  11. /*
  12.    GRAPHICS DEMO FOR TURBO C 2.0
  13.  
  14.    Copyright (c) 1987,88,90 Borland International. All rights reserved.
  15.  
  16.    From the command line, use:
  17.  
  18.       tcc bgidemo graphics.lib
  19.  
  20. */
  21.  
  22. #define CRTtextmode1 0    // set to 1 if you don't want to use InterBGI driver
  23.  
  24. #ifdef __TINY__
  25. #error BGIDEMO will not run in the tiny model.
  26. #endif
  27.  
  28. #include <dos.h>
  29. #include <math.h>
  30. #include <conio.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <stdarg.h>
  34. #include <string.h>
  35. #include <graphics.h>
  36.  
  37. #include "prtgraph.h"
  38. #include "BGIDEMO.H"
  39.  
  40. int    GraphDriver;     /* The Graphics device driver    */
  41. int    GraphMode;    /* The Graphics mode value    */
  42.  
  43. static unsigned  picwidth=4000,picheight=3000;
  44. static int       leftmargin=0,topmargin=0,
  45.                  PicRotate=0, PicInverse=1;
  46. static int  PRTno;
  47. static int  mode;
  48. static char OutName[MAXPATH]="PRN";
  49. struct palettetype palette;      /* Used to read palette info  */
  50. int static  printing=0,asking=0;
  51. int PRT_drv;
  52. int PCXmode=0;
  53.  
  54. int ScreenPreview=1;
  55.  
  56. #define ESC 0x1b        /* Define the escape key   */
  57.  
  58. #include <assert.h>
  59. #include <alloc.h>
  60. #include "userprtf.c"
  61. #include "harderrh.c"
  62.  
  63.  
  64. /*-------------------*/
  65. void grErrorTest(void)
  66. /*-------------------*/
  67. { int ErrorCode;
  68.   ErrorCode = graphresult();     /* Read result of initialization*/
  69.   if( ErrorCode == grOk ) return;
  70.   /* Error occurred  */
  71.   #if CRTtextmode1
  72.      closegraph();
  73.   #else
  74.      BGI_closegraph();
  75.   #endif
  76.   printf(" Graphics System Error: %s\n", PRT_grapherrormsg( ErrorCode ) );
  77.   PRT_End();
  78.   exit( 16 );
  79. }
  80.  
  81. #ifdef __cplusplus
  82.    #define min(x,y) ((x)<=(y)? (x):(y))
  83. #endif
  84.  
  85. /*                         */
  86. /* CHANGETEXTSTYLE: similar to settextstyle, but checks for */
  87. /* errors that might occur whil loading the font file.      */
  88. /*                         */
  89.  
  90. void changetextstyle(int font, int direction, int charsize)
  91. {
  92.   int m;
  93.   int x,y;
  94.  
  95.   graphresult();        /* clear error code     */
  96.   if ( printing && font == DEFAULT_FONT )
  97.   {
  98.       PRT_Resolution ( &x,&y );
  99.       m = min ( y+60/120, getmaxx()/600+1 );
  100.       if (m>1) charsize *=m;
  101.   }
  102.   settextstyle(font, direction, charsize);
  103.   grErrorTest();
  104. }
  105.  
  106.  
  107. int _scanf( const char * format, ... )
  108. {  va_list ap;
  109.    int     c,r;
  110.  
  111.    c=getchar();
  112.    if ( c=='\n' )  return 0;
  113.    va_start(ap,format);
  114.    ungetc(c,stdin);
  115.    r = vscanf (format,ap);  (void)getchar();
  116.    va_end(ap);
  117.    return r;
  118. }
  119.  
  120.  
  121. /*-----------------------*/
  122. void AskOfParameters(void)
  123. /*-----------------------*/
  124. {  int   c,MAXmode;
  125.    const char  *modename;
  126.    char  s[MAXPATH];
  127.  
  128.    clrscr();
  129.    cprintf ( "\n\r Output device name [%s]", OutName );
  130.    gets ( s ); if (*s!=0) strcpy(OutName,s);
  131.    cputs("\r\n");
  132.    PRT_SetOutName ( OutName );
  133.    cputs ( "\r\n Choose printer mode operation\r\n" );
  134.    PRT_MaxMode ( PRTno, &MAXmode );
  135.    for ( mode=0; mode<=MAXmode; mode++ )
  136.    {
  137.       PRT_ModeName(PRTno,mode,&modename );
  138.       cprintf ( "        %2d - %s\r\n", mode, modename );
  139.    }
  140.    mode=MAXmode+1;
  141.    do
  142.    {
  143.      c=getch();
  144.      if (c==0) getch();
  145.      else if ((unsigned)c-'0'<=(unsigned)MAXmode) mode=c-'0';
  146.    }
  147.    while ( mode>MAXmode);
  148.  
  149.    cprintf ( "\n\r Picture width in 1/1000 inch [%d] ", picwidth );
  150.    _scanf ( "%d", &picwidth );
  151.    cprintf ( " Picture height in 1/1000 inch [%d] ", picheight );
  152.    _scanf ( "%d", &picheight );
  153.  
  154.    cprintf ( " Top margin in 1/1000 inch [%d] ", topmargin );
  155.    _scanf ( "%d", &topmargin );
  156.    cprintf ( " Left margin in 1/1000 inch [%d] ", leftmargin );
  157.    _scanf ( "%d", &leftmargin );
  158.    cprintf ( " Rotate picture [%d] ", PicRotate );
  159.    _scanf ( "%d", &PicRotate );
  160.    cprintf ( " Inverse picture [%d] ", PicInverse );
  161.    _scanf ( "%d", &PicInverse );
  162.    cprintf ( " Screen Preview [%d] ", ScreenPreview );
  163.    _scanf ( "%d", &ScreenPreview );
  164.    cprintf ( " PCX mode [%d] ", PCXmode );
  165.    _scanf ( "%d", &PCXmode );
  166. }
  167.  
  168. /*---------------------------------*/
  169. void DrawAndPrint ( int far func(void far * UserPointer), void far* UserPointer )
  170. /*---------------------------------*/
  171. {  int   rc;
  172.    int   PRT_mode=0;
  173.    do
  174.    {
  175.       asking=0;
  176.       printing=0;
  177.       func(UserPointer);
  178.       if ( asking )
  179.       {  int  mode;
  180.          mode = BGI_getgraphmode(Scrn_BGIgroup);
  181.          restorecrtmode();
  182.          AskOfParameters();
  183.          BGI_setgraphmode( mode );
  184.       }
  185.       if ( printing )   /* Have user pressed Ctrl-P ? */
  186.       {
  187.          PRT_SetUserBeforeBuildFunc( BuildingMsg );
  188.          // PRT_SetDriver ( PRTno, mode,picwidth,picheight,
  189.          //                 ( PicRotate ? PRT_ROTATE:0 ) | ( PicInverse ? PRT_INVERSE:0 ) );
  190.          PRT_SetPrinterDrv ( PRTno, mode );
  191.          PRT_SetPictureInch ( picwidth,picheight,
  192.                               ( PicRotate ? PRT_ROTATE:0 ) |
  193.                               ( PicInverse ? PRT_INVERSE:0 ) );
  194.          PRT_SetMargins ( leftmargin, topmargin );
  195.          PRT_HaltPrinting = 0; // reset break indicator
  196.          rc=PRT_PrintBGI ( &PRT_drv, &PRT_mode, getenv("BGIPATH"), func, UserPointer );
  197.          #if CRTtextmode1
  198.             BGI_setgraphmode(mode);
  199.          #endif
  200.          if ( rc )
  201.          {  int  mode;
  202.             mode=BGI_getgraphmode(Scrn_BGIgroup);
  203.             restorecrtmode();
  204.             cprintf ("\r\n error code %d (%s) from PRT_PrintBGI \r\n", rc,
  205.                                     PRT_grapherrormsg(rc) );
  206.             getch();
  207.             BGI_setgraphmode(mode);
  208.          }
  209.       }
  210.    }
  211.    while ( asking || printing );
  212.  
  213. }
  214.  
  215. /*-----------------*/
  216. void  InitPBGI(void)
  217. /*-----------------*/
  218. {
  219.    const char  *PRTname;
  220.    int   MaxPrinterNo;
  221.    int      rc;
  222.  
  223.    rc=PRT_ReadDrivers(getenv("BGIPATH"),"Printers.Def");
  224.    // rc=PRT_LinkDrivers(); 
  225.    if ( rc!=0 )
  226.    {
  227.       cputs ("\r\nSorry - I can't find drivers defintion file\r\n" );
  228.       exit(8);
  229.    }
  230.    clrscr();
  231.    cputs ( "\r\n\n\n"
  232.            "\n\rThis is a sample program (developed from Borland's BGIDEMO.C)"
  233.            "\n\rdemonstrating some of the features of PrintBGI toolkit"
  234.            "\n\rHope you'll find it usefull (the whole package not this program,"
  235.            "\n\rof course).\n\r"
  236.            "\n\rSince this is only an example program for demonstrating purposes"
  237.            "\n\rit uses standard C scanf function to input data with all"
  238.            "\n\rconsequences of it." );
  239.    cputs ( "\r\n"
  240.            "\r\nPlease, let me know if this program does not work with your printer."
  241.            "\r\nTo contact me write to RESZTAK@PLUMCS11.bitnet " );
  242.    cputs ( "\n\r \n\r                   Press any key to continue");
  243.    if ( getch()==0 ) getch();
  244.    clrscr();
  245.  
  246.    MaxPrinterNo = PRT_MaxDriver();
  247.  
  248.    cputs ( "\r\n    Choose printer type\r\n" );
  249.    for ( PRTno=1; PRTno<=MaxPrinterNo; PRTno++ )
  250.    {
  251.       PRT_DriverName(PRTno,&PRTname );
  252.       cprintf ( "        %2d - %s \r\n", PRTno, PRTname );
  253.    }
  254.    do
  255.    {
  256.       scanf( "%d",&PRTno);
  257.       (void)getchar();
  258.    }
  259.    while ( PRTno>MaxPrinterNo || PRTno<=0 );
  260.  
  261.    clrscr();
  262.    PRT_drv = DETECT; /* <-- needed if you don't want link BitImage BGI driver */
  263.    PRT_drv = PRT_installuserdriver ( "BitImage", NULL );
  264.    rc = PRT_registerfarbgidriver ( BitImage );
  265.  
  266.    AskOfParameters();
  267.    cputs ( "\r\n\r\n You will be able to change above parameters by pressing Ctrl-C." );
  268.    cputs ( "\n\r \n\r                   Press any key to continue");
  269.    if ( getch()==0 ) getch();
  270.  
  271.    PRT_SetUserPrintFunc(StdUserPrtFunc);
  272. }
  273.  
  274. #ifdef __cplusplus
  275.    void interrupt (*old_0x1b)(...);
  276. #else
  277.    void interrupt (*old_0x1b)(void);
  278. #endif
  279. void restore_0x1b(void)
  280. {
  281.    setvect(0x1b,old_0x1b);
  282. }
  283. /*                         */
  284. /* Begin main function                 */
  285. /*                         */
  286.